import { Component, OnInit, ViewChild } from '@angular/core';
import { GlobalVariable } from '../../../global.config';
import { HttpRequest } from "../../../service/httpRequest";
import { PageModel } from "../../../model/page-model";
import { flyIn } from '../../../animations/fly-in';
import { ModalDirective } from 'ng2-bootstrap/modal';

@Component({
  selector: 'app-weibao',
  templateUrl: './weibao.component.html',
  styleUrls: ['./weibao.component.css'],
  animations: [
    flyIn
  ]
})
export class WeibaoComponent implements OnInit {

  @ViewChild('myModal') public myModal:ModalDirective;

  tool = GlobalVariable.tooltip;
  data:any;
  pageModel = new PageModel();
  oo:any = {devicename:'',province:'',city:'',district:''};
  page:number = 1;

  selectedId:string = '';
  disabled:string = '';
  buttonType:number = 0;

  constructor(private httpRequest:HttpRequest) { }

  ngOnInit() {
    this.getData();
  }

  getData(){
    this.httpRequest.sendGet('weibao/page',{
      'page': this.page,
      'size': this.pageModel.itemsPerPage,
      'province':this.oo.province,
      'city':this.oo.city,
      'area':this.oo.district,
      'device_name':this.oo.devicename
    }).subscribe(res => {
      //res = this.transData(res);
      this.data = res.result.aaData;
      this.pageModel.setPage({'iTotalRecords':res.result.iTotalRecords});
    },err => console.log(err));
  }


  transData(data){
    for (var i = 0; i < data.result.aaData.length; i++){

      switch (data.result.aaData[i].locate_mode){
        case '0':
          data.result.aaData[i].locate_mode = '输入方式';
          break;
        case '1':
          data.result.aaData[i].locate_mode = 'IP';
          break;
        case '2':
          data.result.aaData[i].locate_mode = 'GPIS';
          break;
        default:
          break;
      }

      switch (data.result.aaData[i].online){
        case '0' :
          data.result.aaData[i].online = '离线';
          break;
        case '1' :
          data.result.aaData[i].online = '在线';
          break;
        default:
          break;
      }
      data.result.aaData[i].did = data.result.aaData[i].id.replace("/","|");
    }

    return data;
  }

  confirmRevoke():void{
    this.disabled='disabled';
    this.httpRequest.sendDelete('weibao/chexiao',{
      weibao_id:this.selectedId
    }).subscribe(res=>{
      alert("撤销成功！");
      this.myModal.hide();
      this.disabled='';
      this.getData();
    },err => {alert('撤销失败!');this.disabled='';console.log(err)});
  }


  confirmDelete():void{
    this.disabled='disabled';
    this.httpRequest.sendDelete('weibao/shanchu',{
      weibao_id:this.selectedId
    }).subscribe(res=>{
      alert("删除成功！");
      this.myModal.hide();
      this.disabled='';
      this.getData();
    },err => {alert('删除失败!');this.disabled='';console.log(err)});
  }


}
